import socket

# 服务器的IP地址和端口
SERVER_IP = '127.0.0.1'
SERVER_PORT = 12345

def main():
    # 创建TCP套接字
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # 绑定IP地址和端口
    server_socket.bind((SERVER_IP, SERVER_PORT))
    # 开始监听
    server_socket.listen(1)
    print(f"服务器正在监听 {SERVER_IP}:{SERVER_PORT}...")

    while True:
        # 接受客户端连接
        client_socket, client_address = server_socket.accept()
        print(f"接受来自 {client_address} 的连接")
        # 接收客户端发送的结果
        result = client_socket.recv(1024).decode()
        print(f"收到结果: {result}")
        # 发送响应给客户端
        response = f"已收到 {result}"
        client_socket.sendall(response.encode())
        # 关闭客户端套接字
        client_socket.close()

if __name__ == "__main__":
    main()